package com.chaodev.base.controller;

import com.github.pagehelper.PageInfo;
import com.chaodev.base.base.RedisDao;
import com.chaodev.base.dto.ButtonDTO;
import com.chaodev.base.model.*;
import com.chaodev.base.service.IButtonService;
import com.chaodev.base.service.ILogService;
import com.chaodev.base.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/button")
@Api("按钮管理")
public class ButtonController {

    @Autowired
    private RedisDao redisDao;

    @Autowired
    private ILogService logService;

    @Autowired
    private IButtonService buttonService;


    @PostMapping(value = "/getButtonList")
    @ApiOperation(value = "获取按钮列表")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result buttonQuery(@RequestBody @ApiParam(value = "分页传入参数") ButtonPageVO dataPage, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            //默认分页设置
            if (dataPage.getPage()==0) {
                dataPage.setPage(1);
            }
            if (dataPage.getRows()==0) {
                dataPage.setRows(20);
            }
            //查询按钮分页列表
            PageInfo<ButtonDTO> pageInfo = buttonService.buttonQueryPage(dataPage);
            Map<String, Object> map = new HashMap<>(2);
            map.put("total", pageInfo.getTotal());
            map.put("rows", pageInfo.getList());
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setData(map);
            result.setCode(0);
            //记录日志
            Result logResult = new Result();
            logResult.setCode(0);
            //不记录返回数据，只做标识
            logResult.setMsg("数据获取成功");
            logResult.setServerID(result.getServerID());
            logService.logAdd(dataPage.toString(), "按钮列表", "SUCCESS", logResult.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(dataPage.toString(), "按钮列表", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * @Description: 功能新增
     * @Author: xy
     * @CreateDate: 2019/12/26 20:04
     * @UpdateUser: xy
     * @UpdateDate: 2019/12/26 20:04
     * @UpdateRemark: 修改内容
     * @Version: 1.0
     */
    @PostMapping(value = "/save")
    @ResponseBody
    @SuppressWarnings("all")
    @ApiOperation(value = "按钮新增/编辑")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result ButtonAdd(@RequestBody @ApiParam(value = "功能信息") Button button, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            button.setCreateUser(userInfo.getUserId());
            button.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            boolean flag = buttonService.buttonAdd(button);
            if (flag == true) {
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("提交成功");
            } else {
                result.setCode(-1);
                result.setMsg("提交失败");
            }
            logService.logAdd(button.toString(), "按钮新增/编辑", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(button.toString(), "按钮新增/编辑", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * @Description: 功能删除
     * @Author: xy
     * @CreateDate: 2019/12/26 16:24
     * @UpdateUser: xy
     * @UpdateDate: 2019/12/26 16:24
     * @UpdateRemark: 修改内容
     * @Version: 1.0
     */
    @PostMapping(value = "/delete")
    @ResponseBody
    @SuppressWarnings("all")
    @ApiOperation(value = "按钮删除")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result ButtonDelete(@RequestBody @ApiParam(value = "功能消息") Button button, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            boolean flag = buttonService.buttonDelete(button);
            if (flag == true) {
                result.setCode(ResultEnum.SUCCESS.getCode());
                result.setMsg("删除成功");
            } else {
                result.setCode(-1);
                result.setMsg("删除失败");
            }
            logService.logAdd(button.toString(), "按钮删除", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(button.toString(), "按钮删除", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/getbtTypeOptions")
    @ResponseBody
    @ApiOperation(value = "获取按钮类型")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getButtonType(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            Map<String, Object> map = new HashMap<>();
            List<ButtonDTO> buttonTypeList = buttonService.getButtonType(map);
            result.setData(buttonTypeList);
            result.setCode(ResultEnum.SUCCESS.getCode());

            Result logResult = new Result();
            logResult.setCode(ResultEnum.SUCCESS.getCode());
            //不记录返回数据，只做标识
            logResult.setMsg("数据获取成功");
            logResult.setServerID(result.getServerID());
            logService.logAdd("", "获取按钮类型", "SUCCESS", logResult.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd("", "获取按钮类型", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/getMenuBtn")
    @ResponseBody
    @ApiOperation(value = "获取菜单权限按钮")
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result getMenuBtn(@RequestBody @ApiParam(value = "功能消息") Button button, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = new UserInfo();
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            Map<String, Object> map = new HashMap<>();
            map.put("P_ROLE_ID", userInfo.getRoleId());
            map.put("P_MENU_ID", button.getMenuId());
            List<ButtonDTO> list = buttonService.getMenuBtn(map);
            result.setData(list);
            result.setCode(ResultEnum.SUCCESS.getCode());

            Result logResult = new Result();
            logResult.setCode(ResultEnum.SUCCESS.getCode());
            //不记录返回数据，只做标识
            logResult.setMsg("数据获取成功");
            logResult.setServerID(result.getServerID());
            logService.logAdd("", "获取菜单权限按钮", "SUCCESS", logResult.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd("", "获取菜单权限按钮", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

}
